package edu.fhsu.csci468;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JDesktopPane;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ControlPanelGUI extends JFrame {
    
//    public static void main(String[] args)
//    {
//        ControlPanelGUI control = new ControlPanelGUI();
//        control.setVisible(true);
//        control.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
//    }

    private static final long serialVersionUID = 1L;
    private JDesktopPane jDesktopPane = null;
    private JButton jButton7 = null;
    private JButton jButton4 = null;
    private JButton jButton1 = null;
    private JButton jButton8 = null;
    private JButton jButton5 = null;
    private JButton jButton2 = null;
    private JButton jButton0 = null;
    private JButton jButton9 = null;
    private JButton jButton6 = null;
    private JButton jButton3 = null;
    private JTextArea jTextArea = null;
    private JButton jButtonAlarm = null;
	private JButton jButtonDisconnect = null;
    private NumberPushed numberPushed;
    private ObjectOutputStream outStream;
    
    public void closeWindow()
    {
    	this.dispose();
    }
    
    /**
     * This is the default constructor
     */
    public ControlPanelGUI(ObjectOutputStream outStream) {
        super();
        this.setLocationRelativeTo(null);
        this.outStream = outStream;
        numberPushed = new NumberPushed();
        initialize();
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(488, 228);
        this.setContentPane(getJDesktopPane());
        this.setTitle("Control Panel");
    }

    /**
     * This method initializes jDesktopPane    
     *     
     * @return javax.swing.JDesktopPane     
     */
    private JDesktopPane getJDesktopPane() {
        if (jDesktopPane == null) {
            jDesktopPane = new JDesktopPane();
            jDesktopPane.add(getJButton7(), null);
             jDesktopPane.add(getJButton4(), null);
            jDesktopPane.add(getJButton1(), null);
            jDesktopPane.add(getJButton8(), null);
            jDesktopPane.add(getJButton5(), null);
            jDesktopPane.add (getJButton2(), null);
            jDesktopPane.add(getJButton0(), null);
            jDesktopPane.add(getJButton9(), null);
            jDesktopPane.add(getJButton6(), null);
            jDesktopPane.add(getJButton3(), null);
            jDesktopPane.add(getJTextArea(), null);
            jDesktopPane.add(getJButtonAlarm(), null);
			jDesktopPane.add(getJButtonDisconnect(), null);
        }
        return jDesktopPane;
    }

    /**
     * This method initializes jButton7    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton7() {
        if (jButton7 == null) {
            jButton7 = new JButton();
            jButton7.setBounds(new Rectangle(15, 15, 46, 31));
            jButton7.addActionListener(numberPushed);
            jButton7.setText("7");
            
        }
        return jButton7;
    }

    /**
     * This method initializes jButton4    
     *     
     * @return javax.swing.JButton     
     */
    private JButton getJButton4() {
        if (jButton4 == null) {
            jButton4 = new JButton();
            jButton4.setBounds(new Rectangle(15, 60, 46, 31));
            jButton4.addActionListener(numberPushed);
            jButton4.setText ("4");
        }
        return jButton4;
    }

    /**
     * This method initializes jButton1    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton1() {
        if (jButton1 == null) {
            jButton1 = new JButton();
            jButton1.setBounds(new Rectangle(15, 105, 46, 31));
            jButton1.addActionListener(numberPushed);
            jButton1.setText("1");
        }
        return jButton1;
    }

    /**
     * This method initializes jButton8    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton8() {
        if (jButton8 == null) {
            jButton8 = new JButton();
            jButton8.setBounds(new Rectangle(75, 15, 46, 31));
            jButton8.addActionListener(numberPushed);
            jButton8.setText("8");
        }
        return jButton8;
    }

    /**
     * This method initializes jButton5    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton5() {
        if (jButton5 == null) {
            jButton5 = new JButton();
            jButton5.setBounds(new Rectangle(75, 60, 46, 31));
            jButton5.addActionListener(numberPushed);
            jButton5.setText("5");
        }
        return jButton5;
    }

    /**
     * This method initializes jButton2    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton2() {
        if (jButton2 == null) {
            jButton2 = new JButton();
            jButton2.setBounds(new Rectangle(75, 105, 46, 31));
            jButton2.addActionListener(numberPushed);
            jButton2.setText ("2");
        }
        return jButton2;
    }

    /**
     * This method initializes jButton0    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton0() {
        if (jButton0 == null) {
            jButton0 = new JButton();
            jButton0.setBounds(new Rectangle(74, 150, 47, 31));
            jButton0.addActionListener(numberPushed);
            jButton0.setText("0");
        }
        return jButton0;
    }

    /**
     * This method initializes jButton9    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton9() {
        if (jButton9 == null) {
            jButton9 = new JButton();
            jButton9.setBounds(new Rectangle(135, 15, 46, 31));
            jButton9.addActionListener(numberPushed);
            jButton9.setText("9");
        }
        return jButton9;
    }

    /**
     * This method initializes jButton6    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton6() {
        if (jButton6 == null) {
            jButton6 = new JButton();
            jButton6.setBounds(new Rectangle(135, 60, 46, 31));
            jButton6.addActionListener(numberPushed);
            jButton6.setText("6");
        }
        return jButton6;
    }

    /**
     * This method initializes jButton3    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton3() {
        if (jButton3 == null) {
            jButton3 = new JButton();
            jButton3.addActionListener(numberPushed);
            jButton3.setBounds(new Rectangle(135, 105, 46, 31));
            jButton3.setText ("3");
        }
        return jButton3;
    }
        
    /**
     * This method initializes jTextArea    
     *     
     * @return javax.swing.JTextArea    
     */
    private JTextArea getJTextArea() {
        if (jTextArea == null) {
            jTextArea = new JTextArea();
            jTextArea.setEditable(false);
            jTextArea.setBorder(BorderFactory.createEtchedBorder ());
            jTextArea.setBounds(new Rectangle(196, 15, 253, 121));
        }
        return jTextArea;
    }
    
    public void setJTextArea(String message)
    {
    	jTextArea.setText(message);
    }
    
	/**
	 * This method initializes jButtonAlarm	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAlarm() {
		if (jButtonAlarm == null) {
			jButtonAlarm = new JButton();
			jButtonAlarm.addActionListener(new Panic());
			jButtonAlarm.setBounds(new Rectangle(195, 150, 121, 31));
			jButtonAlarm.setText("Panic");
		}
		return jButtonAlarm;	
	}
    
	/**
	 * This method initializes jButtonDisconnect	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDisconnect() {
		if (jButtonDisconnect == null) {
			jButtonDisconnect = new JButton();
            jButtonDisconnect.addActionListener(new Disconnect());		
			jButtonDisconnect.setBounds(new Rectangle(330, 150, 121, 31));
			jButtonDisconnect.setText("Disconnect");
		}
		return jButtonDisconnect;
	}
	
    private class NumberPushed implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		try
    		{
    			outStream.writeObject(Integer.decode(event.getActionCommand()));
    			outStream.flush();
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    		//System.out.println(Integer.decode(event.getActionCommand())+100);
    	}
    }
    
    private class Panic implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		Integer a = -2;
    		try
    		{
    			outStream.writeObject(a);
    			outStream.flush();
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    
    private class Disconnect implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		Integer a = -1;
    		try
    		{
    			outStream.writeObject(a);
    			outStream.flush();
    			closeWindow();
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }

}  //  @jve:decl-index=0:visual-constraint="132,60"